unit F_Processing;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, SyncObjs, ComCtrls, StdCtrls;

type
  TfrmProcessing = class(TForm)
    pnlFormClient: TPanel;
    Animate: TAnimate;
    lblTitle: TLabel;
    lblContent: TLabel;
    imgBackGroud: TImage;
  private
    function GetContent: String;
    function GetIsVisible: Boolean;
    function GetPlaying: Boolean;
    function GetTitle: String;

    procedure SetContent(const Value: String);
    procedure SetTitle(const Value: String);

    procedure MoveToWorkAreaCenter;

  protected
    procedure DoCreate; override;

  public
    procedure MakeInvisible;
    procedure MakeVisible;
    procedure Play;
    procedure Stop;

    property Content: String read GetContent write SetContent;
    property IsVisible: Boolean read GetIsVisible;
    property Playing: Boolean read GetPlaying;
    property Title: String read GetTitle write SetTitle;

  end;

  function IsProcessing: Boolean;
  procedure BeginProcess(sTitle: String = ''; sContent: String = '');
  procedure EndProcess;

implementation

{$R *.dfm}
{$R avi.res}

var

  frmProcessing_: TfrmProcessing;
  ACriticalSection: TCriticalSection;


function ProcessingForm: TfrmProcessing;
begin
  if frmProcessing_ = nil then
    frmProcessing_:= TfrmProcessing.Create(nil);

  result:= frmProcessing_;
end;

procedure BeginProcess(sTitle: String = ''; sContent: String = '');
begin
  if not IsProcessing then
  begin
    ProcessingForm.MakeVisible;
    ProcessingForm.Play;
  end;

  if sTitle <> '' then
    ProcessingForm.Title:= sTitle;

  if sContent <> '' then
    ProcessingForm.Content:= sContent;

  ProcessingForm.BringToFront;

  Application.ProcessMessages;
end;

procedure EndProcess;
begin
  if IsProcessing then
  begin
    ProcessingForm.Stop;
    ProcessingForm.MakeInvisible;
    ProcessingForm.Caption:= '';
    ProcessingForm.Title:= '';
  end;
end;

function IsProcessing: Boolean;
begin
  result:= ProcessingForm.IsVisible;
end;

{ TfrmProcessing }

procedure TfrmProcessing.DoCreate;
begin
  inherited;

  MakeInvisible;
  Visible:= true;

  Title:= '';
  Content:= '';

  Animate.ResName:= 'AVI_PROCESSING';
end;

function TfrmProcessing.GetContent: String;
begin
  result:= lblContent.Caption; 
end;

function TfrmProcessing.GetIsVisible: Boolean;
begin
  result:= (Left >= 0) and (Top >= 0);
end;

function TfrmProcessing.GetPlaying: Boolean;
begin
  result:= Animate.Active;
end;

function TfrmProcessing.GetTitle: String;
begin
  result:= lblTitle.Caption;
end;

procedure TfrmProcessing.MakeInvisible;
begin
  Left:= -10000;
  Top:= -10000;
end;

procedure TfrmProcessing.MakeVisible;
begin
  MoveToWorkAreaCenter;
end;

procedure TfrmProcessing.MoveToWorkAreaCenter;
var
  nLeft, nTop: Integer;

begin
  nLeft:= (Screen.WorkAreaWidth- Width) div 2;
  nTop:= (Screen.WorkAreaHeight- Height) div 2;

  BoundsRect:= Rect(nLeft, nTop, nLeft+ Width, nTop+ Height);
end;

procedure TfrmProcessing.Play;
begin
  Animate.Active:= true;
end;

procedure TfrmProcessing.SetContent(const Value: String);
begin
  lblContent.Caption:= Value; 
end;

procedure TfrmProcessing.SetTitle(const Value: String);
begin
  lblTitle.Caption:= Value; 
end;

procedure TfrmProcessing.Stop;
begin
  Animate.Active:= false;
end;

initialization

  ACriticalSection:= TCriticalSection.Create;

finalization

  FreeAndNil(ACriticalSection);

  if frmProcessing_ <> nil then
    FreeAndNil(frmProcessing_);

end.
